﻿using System;
using System.Drawing;
using System.IO;
using System.Net;

namespace Useful
{
    public static class GoogleMap
    {
        private static string Convert(string str)
        {
            return str.Replace(' ', '+');
        }

        private static string CreateAddrString(string country, string townType, string town, string streetType, string street, string house)
        {
            string addr =  String.Format("{0},{1} {2},{3} {4},{5}", country, townType, town, streetType, street, house);

            return Convert(addr);
        }

        private static string GetHttpStringAddr(string country, string townType, string town, string streetType, string street, string house)
        {
            string strAddr = CreateAddrString(country, townType, town, streetType, street, house);

            if (!String.IsNullOrEmpty(strAddr))
                return String.Format("http://maps.google.com/maps/api/staticmap?center={0}&size=800x800&sensor=false&markers=color:blue%7Clabel:S%7C{0}", strAddr);
            else
                return null;
        }

        public static string CreateHttpMapString(string country, string townType, string town, string streetType, string street, string house)
        {
            return GetHttpStringAddr(country, townType, town, streetType, street, house);
        }

        public static Image GetMapImage(string country, string townType, string town, string streetType, string street, string house)
        {
            string httpRequest = GetHttpStringAddr(country, townType, town, streetType, street, house);

            if (httpRequest != null)
            {
                httpRequest += "&language=ru";
                WebClient client = new WebClient();

                byte[] bytes = client.DownloadData(httpRequest);

                MemoryStream ms = new MemoryStream(bytes);

                return Image.FromStream(ms);
            }
            else
                return null;
        }
    }
}
